//
// Created by 谢哲 on 2021/5/17.
//

#include <iostream>
#include <set>
using namespace std;

int main() {
    int n;
    cin >> n;
    multiset<int> sdata;
    sdata.insert(-2147483647);
    sdata.insert(2147483647);

    for (int i=0; i<n; ++i) {
        int a, b;
        cin >> a >> b;
        if (a == 1) {
            int cnt = 0;
            for (auto p=sdata.begin(); *p<b; ++p, ++cnt);
            cout << cnt << "\n";
        }
        else if (a==2) {
            auto p = sdata.begin();
            for (; b; --b, ++p);
            cout << *p << "\n";
        }
        else if (a==3) {
            auto p = sdata.lower_bound(b);
            --p;
            cout << *p << "\n";
        }
        else if (a==4) {
            auto p = sdata.upper_bound(b);
            cout << *p << "\n";
        }
        else {
            sdata.insert(b);
        }
    }

    return 0;
}
